package com.pkq.one;

import java.util.*;

/**
 * @author ピカチュウ(皮卡啾)
 * @date 2021/7/9 15:07
 * @Description: java去除数组中重复元素，并按字典顺序从小到大排序
 */

public class Deduplication {

    // 1. 集合的contains方法去判断重复元素
    public List newNums1(Object[] nums) {
        Arrays.sort(nums);

        ArrayList<Object> list = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            if (list.contains(nums[i])) {
                continue;
            } else list.add(nums[i]);
        }

        return list;
    }

    // 2. 利用Set无序不重复的特性进行去重
    public List newNums2(Object[] nums) {
        Arrays.sort(nums);

        HashSet<Object> set = new HashSet<>();

        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        return Arrays.asList(set.toArray());
    }

    // 3. 使用LinkedHashSet，有顺序、不重复
    public List newNums3(Object[] nums) {
        Arrays.sort(nums);

        LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();

        for (int i = 0; i < nums.length; i++) {
            linkedHashSet.add(nums[i]);
        }
        return Arrays.asList(linkedHashSet.toArray());
    }
}
